package com.aischool.eduorg.admin;

import java.util.List;
import java.util.stream.Collectors;

import com.aischool.routes.RouteMapping;
import com.aischool.service.AiOrgService;
import com.aischool.service.SysAppService;
import com.aischool.service.SysPermissionService;
import com.aischool.utils.AppUtil;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/admin/eduorg")
public class EduorgController extends Controller {
	
	@Inject
	SysAppService sysAppService;
	@Inject
	SysPermissionService sysPermissionService;
	@Inject
	AiOrgService aiOrgService;

	private final String view_path = "/eduorg/";
	
	public void welcome() {
		render("/eduorg/welcome.html");
	}
	
	/**
	 * 登录 /admin/login/4
	 */
	public void login() {
		int app_id = getParaToInt(0, 0);
		if (app_id > 0) {
			Record rd = sysAppService.getSysApp(app_id);
			if (rd != null) {
				this.setAttr("login_app_id", app_id);
				this.setAttr("login_app_name", rd.getStr("app_name"));
				render(view_path + "login.html");
				return ;
			}
		}
		renderText("页面丢失！");
	}
	
	public void index() {
		int user_id = this.getAttrForInt("login_user_id");
		int app_id = this.getAttrForInt("login_app_id");
		int org_id = this.getAttr("login_org_id", 0);
		System.err.println(org_id);
		if (org_id > 0) {
			Record rd = aiOrgService.getOrg(org_id);
			System.err.println(rd);
			if (rd != null) {
				this.setAttr("index_org_name", rd.get("org_name"));
			}
		}
		
		if (app_id > 0 && user_id>0) {
			String url_welcome = "/admin/welcome";
			if (app_id == 4) {
				url_welcome = "/admin/eduorg/welcome";
			}
			this.setAttr("attr_welcome_url", url_welcome);
			
			List<Record> listmenu = sysPermissionService.getAppUserMenu(app_id, user_id);
			List<Record> listm1 = listmenu.stream()
					.filter((Record r) -> r.getInt("parent_id")==0)
					.collect(Collectors.toList());
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < listm1.size(); i++) {
				Record r1 = listm1.get(i);
				if (r1.getBoolean("is_hide")) {
					continue;
				}
				sb.append("<li class=\"layui-nav-item");
				//sb.append(" layui-nav-itemed");
				if (i==0) {
					sb.append(" layui-nav-itemed");
				}
				sb.append("\">");
				sb.append("<a href=\"javascript:;\"><i class=\"iconfont\">"+r1.getStr("icon_code")+"</i>"+r1.getStr("menu_name")+"</a>");
				List<Record> listm2 = listmenu.stream()
						.filter((Record r) -> r.getInt("parent_id")==r1.getInt("menu_id"))
						.collect(Collectors.toList());
				if (listm2 != null && listm2.size()>0) {
					sb.append("<dl class=\"layui-nav-child\">");
					for(int j=0; j < listm2.size(); j++) {
						Record r2 = listm2.get(j);
						if (r2.getBoolean("is_hide")) {
							continue;
						}
						sb.append("<dd><a href=\"javascript:;\" data-url=\""+AppUtil.addUrlParams(r2.getStr("menu_url"), "app_id="+app_id)+"\" "
								+ "data-id=\""+r2.getStr("menu_id")+"\" data-text=\""+r2.getStr("menu_name")+"\"><span class=\"l-line\"></span>"+r2.getStr("menu_name")+"</a></dd>");
					}
					sb.append("</dl>");
				}
				
				sb.append("</li>");
			}
			this.setAttr("listmenuhtml", sb.toString());
		}
		
		if (app_id > 0) {
			Record rd = sysAppService.getSysApp(app_id);
			if (rd != null) {
				this.setAttr("login_app_name", rd.getStr("app_name"));
				render(view_path + "index.html");
				return ;
			}
		}
		renderText("页面丢失！");
	}
		
	public void list() {
		setParamsToAttr();
		String mname = getViewName();
		render(mname + "-list.html");
	}
	
	public void edit() {
		setParamsToAttr();
		String mname = getViewName();
		render(mname + "-edit.html");
	}
	
	private String getViewName() {
		String absUrl = getRequest().getRequestURI();
		String[] array1 = absUrl.split("/");
		String mname = view_path + array1[array1.length-1];
		return mname;
	}
	

	private void setParamsToAttr() {
		setAttr("ar_typeid", getPara("typeid",""));
		setAttr("ar_poid", getPara("poid",""));
	}
}
